package eu.telecombretagne.rsm.network;

import java.util.concurrent.*;
import java.io.*;
import eu.telecombretagne.rsm.base.event.*;
import eu.telecombretagne.rsm.base.event.manager.EventService;
import eu.telecombretagne.rsm.gui.NameConsts;
import eu.telecombretagne.rsm.gui.frames.DesktopFrame;

public class ReaderThread extends Thread {
	
	// private Socket _s;
	private ArrayBlockingQueue<Event> _v;
	private ThreadStatus _ts;
	private ObjectInputStream _ois;
	
	
	public ReaderThread (ArrayBlockingQueue<Event> v, ThreadStatus ts, ObjectInputStream ois) {
		_v = v;
		_ts = ts;
		_ois = ois;
	}
	
	public void run(){
		try {
			while (true) {
				Event e = (Event) _ois.readObject();
				EventService.putEventToReaderQueue(e, _v);
			}
		} catch(IOException ioe){
			DesktopFrame.writeLogError(this.getClass().getSimpleName() + NameConsts.DETECTINGTHEENDOFAPROCESS);
			_ts.readerisOn = false;
			_ts.writerisOn = false;

		} catch(ClassNotFoundException cnfe){
			DesktopFrame.writeLogError(this.getClass().getSimpleName() + ": " + cnfe);
			System.exit(1);
		} catch(InterruptedException ie){
			DesktopFrame.writeLogError(this.getClass().getSimpleName() + ": " + ie);
			System.exit(1);
		}
	}
}